package com.pinxiang.framework.weather.power.controller;


import com.alibaba.fastjson.JSON;
import com.pinxiang.framework.weather.power.model.Equipment;
import com.pinxiang.framework.weather.power.service.IEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by spc on 2018/5/31.
 */
@Controller
@RequestMapping(value = "/equ")
public class EquipmentController {
    @Autowired
    private IEquipmentService equipmentService;

    @RequestMapping(value = "/dataGrid")
    @ResponseBody
    public Map<String, Object> query(int limit, int offset, String page, String search) {
        Map<String, Object> parameters = new HashMap<String, Object>();

        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Equipment> users = equipmentService.findByParams(parameters);
        resultMap.put("data", users);

        return resultMap;
    }

    @RequestMapping(value = "/loadRolseData")
    @ResponseBody
    public String loadRolseData() {
        String result = "";
        List<Object> list = new ArrayList<Object>();
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        //查询本年度 本单位各个机型的"完成架次"
        List<Equipment> rolseList = equipmentService.findGroupBycategory();
        String[] legend = new String[rolseList.size()];
        int i = 0;
        for (Equipment eq : rolseList) {
            Map<String, Object> resultMap = new HashMap<String, Object>();
            String category = eq.getCategory();
            String sl = eq.getValid();
            legend[i++] = category;
            resultMap.put("name", category);
            resultMap.put("value", Integer.valueOf(sl));
            mapList.add(resultMap);
        }
        list.add(mapList);
        list.add(legend);
        result = JSON.toJSONString(list);

        return result;
    }


    @RequestMapping(value = "/loadDevice")
    @ResponseBody
    public Map loadDevicec(String category) {
        Map<String, Object> resultmap = new HashMap<String, Object>();
        Map<String,String> params = new HashMap<String, String>();
        params.put("category",category);
        List<Equipment> deviceList = equipmentService.findGroupDevice(params);
        resultmap.put("dev", deviceList);
        return resultmap;
    }

    @RequestMapping(value = "/loadEqu")
    @ResponseBody
    public Map loadEqu(String devicecode) {
        Map<String, Object> resultmap = new HashMap<String, Object>();
        List<Equipment> deviceList = equipmentService.findGroupEqu(devicecode);
        resultmap.put("equ", deviceList);
        return resultmap;
    }

}
